package com.projects.android.bus;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import org.json.JSONStringer;

import android.util.Log;

import com.projects.android.dto.UsersDTO;
import com.projects.android.utils.Utils;

public class UsersBUS {
	public static UsersDTO selectByUserName(String username) {
		UsersDTO myData = null;
		try {
			Map<String, String> mapUser = new HashMap<String, String>();
			mapUser.put("username", username);
			String url = Utils.buildURL(Utils.SERVICE_URI
					+ "/Users_SelectByUserName", mapUser);

			// Send GET request to <service>/GetPlates
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet request = new HttpGet(new URI(url));
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");

			HttpResponse response = httpClient.execute(request);
			HttpEntity responseEntity = response.getEntity();
			// Read response data into buffer
			String result = Utils.getResponse(responseEntity);
			Log.i("UsersBUS selectByUserName", "Response: " + result);

			if (result != "") {
				JSONObject data = new JSONObject(result);
				myData = new UsersDTO();
				myData.initObject(data);
			}
		} catch (Exception e) {
			Log.e("UsersBUS selectByUserName", e.toString());
		}
		return myData;
	}
	
	public static String insert(UsersDTO data) {
		try {
			// POST request to <service>/SaveVehicle
			HttpPost request = new HttpPost(new URI(Utils.SERVICE_URI
					+ "/Users_Insert"));
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");

			// Build JSON string
			JSONStringer input = new JSONStringer()
			.object()
				.key("username").value(data.getUserName())
				.key("password").value(data.getPassword())
				.key("email").value(data.getEmail())
				.key("role").value("customer")
				.key("name").value(data.getName())
				.key("phone").value(data.getPhone())
				.key("address").value(data.getAddress())
			.endObject();
			
			StringEntity entity = new StringEntity(input.toString());
			request.setEntity(entity);

			// Send request to WCF service
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpResponse response = httpClient.execute(request);

			HttpEntity responseEntity = response.getEntity();
			// Read response data into buffer
			String result = Utils.getResponse(responseEntity);
			result = result.replace('"', ' ').trim();
			//JSONObject jsonData = new JSONObject(result);
			//result = jsonData.getString("Users_InsertResult");
			Log.i("UsersBUS insert", "Response: " + result);
			
			return result;
		} catch (Exception e) {
			Log.e("insertData", e.toString());
			return e.toString();
		}
	}
	
	public static String resetPassword(String username, String email) {
		try {
			// POST request to <service>/SaveVehicle
			HttpPost request = new HttpPost(new URI(Utils.SERVICE_URI
					+ "/Users_ResetPassword"));
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");

			// Build JSON string
			JSONStringer input = new JSONStringer()
			.object()
				.key("username").value(username)
				.key("email").value(email)
			.endObject();
			
			StringEntity entity = new StringEntity(input.toString());
			request.setEntity(entity);

			// Send request to WCF service
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpResponse response = httpClient.execute(request);

			HttpEntity responseEntity = response.getEntity();
			// Read response data into buffer
			String result = Utils.getResponse(responseEntity);
			result = result.replace('"', ' ').trim();
			//JSONObject jsonData = new JSONObject(result);
			//result = jsonData.getString("Users_InsertResult");
			Log.i("UsersBUS resetPassword", "Response: " + result);
			
			return result;
		} catch (Exception e) {
			Log.e("resetPassword", e.toString());
			return e.toString();
		}
	}
	
	public static boolean changePassword(String username, String oldpassword, String newpassword) {
		try {
			// POST request to <service>/SaveVehicle
			HttpPost request = new HttpPost(new URI(Utils.SERVICE_URI
					+ "/Users_ChangePassword"));
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");

			// Build JSON string
			JSONStringer input = new JSONStringer()
			.object()
				.key("username").value(username)
				.key("oldpassword").value(oldpassword)
				.key("newpassword").value(newpassword)
			.endObject();
			
			StringEntity entity = new StringEntity(input.toString());
			request.setEntity(entity);

			// Send request to WCF service
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpResponse response = httpClient.execute(request);

			HttpEntity responseEntity = response.getEntity();
			// Read response data into buffer
			String result = Utils.getResponse(responseEntity);
			//JSONObject jsonData = new JSONObject(result);
			//result = jsonData.getString("Users_InsertResult");
			Log.i("UsersBUS changePassword", "Response: " + result);
			
			return Boolean.parseBoolean(result);
		} catch (Exception e) {
			Log.e("resetPassword", e.toString());
			return false;
		}
	}
	
	public static boolean update(UsersDTO data) {
		try {
			// POST request to <service>/SaveVehicle
			HttpPost request = new HttpPost(new URI(Utils.SERVICE_URI
					+ "/Users_Update"));
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");

			// Build JSON string
			JSONStringer input = new JSONStringer()
				.object()
					.key("obj")
						.object()
							.key("ID").value(data.getID())
							.key("Email").value(data.getEmail())
							.key("Name").value(data.getName())
							.key("Phone").value(data.getPhone())
							.key("Address").value(data.getAddress())
							.endObject()
						.endObject();
			
			StringEntity entity = new StringEntity(input.toString());
			request.setEntity(entity);

			// Send request to WCF service
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpResponse response = httpClient.execute(request);

			HttpEntity responseEntity = response.getEntity();
			// Read response data into buffer
			String result = Utils.getResponse(responseEntity);
			Log.i("UsersBUS update", "Response: " + result);
			Log.i("UsersBUS update", "Update : "
					+ response.getStatusLine().getStatusCode());
			
			return Boolean.parseBoolean(result);

		} catch (Exception e) {
			Log.e("UsersBUS Update", e.toString());
			return false;
		}
	}
}
